package com.junmeng.jdemo.recycler.chat;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.base.BaseActivityDelegate;
import com.junmeng.jdemo.databinding.ActivityChatDemoBinding;

/**
 * 模拟聊天页
 */
public class ChatDemoActivity extends BaseActivityDelegate {
    ActivityChatDemoBinding binding;
    ChatAdapter chatAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChatDemoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        binding.recyclerView.setAdapter(chatAdapter = new ChatAdapter());
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        chatAdapter.register(new CommonBindViewText());
        chatAdapter.register(new CommonBindViewImage());
        chatAdapter.setOnItemClickLitener((v, position, o) -> showToast("click position=" + position));
        chatAdapter.setOnItemLongClickListener((v, position, o) -> {
            showToast("long click position=" + position);
            return true;
        });

        binding.swipeRefreshLayout.setOnRefreshListener(() -> {
            binding.swipeRefreshLayout.postDelayed((Runnable) () -> {
                final int num = 5;
                for (int i = 0; i < num; i++) {
                    if (i % 2 == 0) {
                        chatAdapter.addItemToFront(new CommonBindViewText.Item(0, chatAdapter.getItemCount() + ""));
                        chatAdapter.addItemToFront(new CommonBindViewImage.Item(1, chatAdapter.getItemCount() + ""));
                    } else {
                        chatAdapter.addItemToFront(new CommonBindViewImage.Item(0, chatAdapter.getItemCount() + ""));
                        chatAdapter.addItemToFront(new CommonBindViewText.Item(1, chatAdapter.getItemCount() + ""));
                    }
                }
                chatAdapter.notifyItemRangeInserted(0, num * 2);
                binding.swipeRefreshLayout.setRefreshing(false);
            }, 1000);//模拟延时1秒

        });

    }

    public void onClickReceive(View view) {
        boolean isScrollToBottom = chatAdapter.isScrollToBottom(binding.recyclerView);
        int rand = (int) (Math.random() * 10);
        if (rand % 2 == 0) {
            chatAdapter.addItem(new CommonBindViewText.Item(0, chatAdapter.getItemCount() + ""));
        } else {
            chatAdapter.addItem(new CommonBindViewImage.Item(0, chatAdapter.getItemCount() + ""));
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        if (isScrollToBottom) {
            chatAdapter.smoothScrollToBottom(binding.recyclerView);
        }
    }

    public void onClickSend(View view) {
        int rand = (int) (Math.random() * 10);
        if (rand % 2 == 0) {
            chatAdapter.addItem(new CommonBindViewText.Item(1, chatAdapter.getItemCount() + ""));
        } else {
            chatAdapter.addItem(new CommonBindViewImage.Item(1, chatAdapter.getItemCount() + ""));
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        chatAdapter.smoothScrollToBottom(binding.recyclerView);
    }

    public void onClickBack(View view) {
        onBackPressed();
    }

    public void onClickSystem(View view) {
    }
}